from GradeSearth import searchTools
from GradeSearth import FileSaveTool
import os
'''考生文理科'''
StuTypes = "综合"
'''填写考生目标院校所在地'''
provincename = "湖南"
'''填写考生所在地'''
###注意：若为直辖市，需要将16行与19行的省改为市
studentProvince = "浙江"
'''mode=0时需要激活provincename的值，根据省份获取全省投档线'''
'''mode=其他值时获取全国投档线'''
mode = 0
#mode = 2

pre_filename = studentProvince+StuTypes+'---'+provincename+"省学校"
def runProvince(prov):
    puredata = []
    provincelist = searchTools.searchSchoolNameByProV(prov + "省")
    for i in provincelist:
        single = searchTools.searchGradeBySchool(provincename, i, studentProvince, StuTypes)
        searchTools.random_sleep()
        print("---------" + i + "---------")
        print("年份    最低分   批次线   录取层次")
        for index, val in enumerate(single):
            print(val, end="    ")
            if ((index + 1) % 4 == 0):
                print("")
        temp = len(single)/4
        for nums in range(int(temp)):
            #temp_dict["province"] = prov
            #temp_dict["schoolname"] =
            #此处一定要在循环体内部初始化，否则由于append的机制，会导致插入的内容一样
            temp_dict = {"province": prov, "schoolname": i, "years": "", "low": "", "batchline": "", "batchname": ""}
            temp_dict["years"] = single[nums*4]
            temp_dict["low"] = single[nums*4+1]
            temp_dict["batchline"] = single[nums*4+2]
            temp_dict["batchname"] = single[nums*4+3]
            #print("wait to add:",temp_dict)
            puredata.append(temp_dict)
            #print("now puredata:",puredata)
    os.chdir(r'E:\pythonProject\GradeSearth\result')
    FileSaveTool.xw_toExcel(data=puredata,filen=pre_filename)
def runAllSchool():
    puredata = []
    provincedict = searchTools.searchSchoolName()
    for school,province in provincedict.items():
        temp_dict = {"province": province, "schoolname": school, "years": "", "low": "", "batchline": "", "batchname": ""}
        single = searchTools.searchGradeBySchool(province, school, studentProvince, StuTypes)
        searchTools.random_sleep()
        print("---------" + school + "---------")
        print("年份    最低分   批次线   录取层次")
        for index, val in enumerate(single):
            print(val, end="    ")
            if ((index + 1) % 4 == 0):
                print("")
        temp = len(single) / 4
        for nums in range(int(temp)):
            # temp_dict["province"] = prov
            # temp_dict["schoolname"] = i
            temp_dict["years"] = single[nums * 4]
            temp_dict["low"] = single[nums * 4 + 1]
            temp_dict["batchline"] = single[nums * 4 + 2]
            temp_dict["batchname"] = single[nums * 4 + 3]
            puredata.append(temp_dict)
    FileSaveTool.xw_toExcel(data=puredata, fileName=studentProvince+StuTypes+"---全国高校")
def main(mode):
    if(mode == 0):
        runProvince(provincename)
    else:
        runAllSchool()

if __name__ == '__main__':
    main(mode)